package com.itheima.studentSysteam;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作：1登录 2注册 3忘记密码 4退出");
            String choose = scanner.next();
            switch (choose) {
                case "1" -> login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                case "4" -> {
                    System.out.println("欢迎使用");
                    // 推出虚拟器
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }

    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.next();
        boolean flag = container(list, username);
        if(!flag) {
            System.out.println("用户名" + username + "不存在，请先注册");
            return;
        }

        System.out.println("请输入身份证号码");
        String personID = scanner.next();
        System.out.println("请输入手机号码");
        String phoneNumber = scanner.next();

        int index = findeIndex(list, username);
        if(index == -1) {
            System.out.println("用户不存在");
            return;
        }
        User user = list.get(index);
        if(!(personID.equalsIgnoreCase(user.getPersonId()) && phoneNumber.equals(user.getPhoneNumber()))) {
            System.out.println("输入的身份证或者手机号不正确");
            return;
        }

        System.out.println("请输入新密码");
        String password = scanner.next();
        user.setPassword(password);

        System.out.println("修改密码成功");


    }

    private static int findeIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            if(username.equals(list.get(i).getUsername())) {
                return i;
            }
        }
        return -1;
    }

    private static void register(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);
        String phoneNumber;
        String personId;
        String password2;
        String username;
        while (true) {
            System.out.println("请输入用户姓名");
            username = scanner.next();
            boolean flag1 = checkUsername(username);
            if(!flag1) {
                System.out.println("用户名格式不满足条件，需要重新输入");
                continue;
            }
            boolean flag2 = container(list, username);
            if(flag2) {
                System.out.println("输入用户名已经存在，请重新输入");
            } else {
                System.out.println("用户名格式正确");
                break;
            }
        }

        while (true) {
            System.out.println("请输入密码");
            String password = scanner.next();
            System.out.println("请再次输入密码");
            password2 = scanner.next();
            if(!password.equals(password2)) {
                System.out.println("两次密码输入不一直，请重新输入");
                continue;
            } else {
                System.out.println("两次密码输入一致");
                break;
            }
        }

        while (true) {
            System.out.println("请输入身份证号码");
            personId = scanner.next();
            boolean flag = checkPersonId(personId);
            if(!flag) {
                System.out.println("输入的身份证不符合格式，请重新输入");
            } else {
                System.out.println("身份证号码输入正确");;
                break;
            }
        }

        while (true) {
            System.out.println("请输入手机号");
            phoneNumber = scanner.next();
            boolean flag = checkPhoneNumber(phoneNumber);
            if(!flag) {
                System.out.println("输入的手机格式不争取");
            } else {
                System.out.println("手机格式输入正确");
                break;
            }
        }
        User user = new User(username, password2, personId, phoneNumber);
        System.out.println("注册成功");
        list.add(user);
        printUsr(list);


    }

    private static void printUsr(ArrayList<User> list) {
        for (User user1 : list) {
            System.out.println(user1.toString());
        }
    }

    private static boolean checkPhoneNumber(String phoneNumber) {
        // 长度为11位
        if(phoneNumber.length() != 11) {
            return false;
        }
        // 不能以0为开头
        if(phoneNumber.startsWith("0")) {
            return false;
        }
        // 必须都是数字
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if(!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;
    }

    private static boolean checkPersonId(String personId) {
        // 长度为18位
        if(personId.length() != 18) {
            return false;
        }
        // 不能以0为开头
        if(personId.startsWith("0")) {
            return false;
        }
        // 前17位，必须都是数字
        for (int i = 0; i < personId.length() - 1; i++) {
            char c = personId.charAt(i);
            if(!(c >= '0' && c <= '9')) {
                return false;
            }
        }

        // 最为一位可以是数字，也可以是大写X或小写x
        char endChar = personId.charAt(personId.length() - 1);
        if((endChar >= '0' && endChar <= '9') || (endChar == 'x') || (endChar == 'X')) {
            return true;
        }

        return false;
    }

    private static boolean container(ArrayList<User> list, String username) {
        for (User user : list) {
            String name = user.getUsername();
            if (username.equals(name)) {
                return true;
            }
        }
        return false;
    }

    private static boolean checkUsername(String username) {
        int length = username.length();
        // 用户名长度必须在3~15位之间
        if(length < 3 || length > 15) {
            return false;
        }

        // 只能是字母加数字的组合
        for (int i = 0; i < length; i++) {
            char c = username.charAt(i);
            if(!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }

        // 不能是纯数字
        int count = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }
        }
        return count > 0;
    }

    private static void login(ArrayList<User> list) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名");
            String username = scanner.next();
            boolean flag = container(list, username);
            if(!flag) {
                System.out.println("用户名" + username + "不存在，请注册之后在登录");
                return;
            }
            System.out.println("请输入密码");
            String password = scanner.next();

            while (true) {
                String rightCode = getCode();
                System.out.println("当前正确的验证码：" + rightCode);
                System.out.println("请输入验证码");
                String code = scanner.next();
                if(!code.equalsIgnoreCase(rightCode)) {
                    System.out.println("输入的验证码不正确，请重新输入");
                } else {
                    System.out.println("输入的验证码正确");
                    break;
                }
            }
            // 传参数建议传入一个对象
            User user = new User(username, password, null, null);
            Boolean flag1 = checkUserInfo(list, user);
            if(!flag1) {
                System.out.println("登录失败");
                if(i == 2) {
                    System.out.println("登录失败3次，禁止登录");
                } else {
                    System.out.println("登录失败" + (2 - i) + "次");
                }
            } else {
                System.out.println("登录成功");
            }
        }

    }

    private static Boolean checkUserInfo(ArrayList<User> list, User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        for (User user1 : list) {
            if(username.equals(user1.getUsername()) && password.equals(user1.getPassword())) {
                return true;
            }
        }
        return false;
    }

    // 生成验证码
    private static String getCode() {
        // 生成 a ~ z A ~ Z 的字节数组
        ArrayList<Character> chars = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            chars.add((char)('a' + i));
            chars.add((char)('A' + i));
        }

        // 获取随机4位字符
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            // 获取随机索引
            int index = random.nextInt(chars.size());
            // 通过随机索引获取随机字符
            char c = chars.get(index);
            // 拼接字符
            code.append(c);
        }
        // 生成 0 到 9 的随机数字
        int num = random.nextInt(10);
        code.append(num);
        // 创建验证码的随机索引
        int index = random.nextInt(code.length());
        // 把字符串转为字符数组，在数组中修改，然后再创建一个新的字符串
        char[] charArray = code.toString().toCharArray();
        char temp = charArray[index];
        charArray[index] = charArray[charArray.length - 1];
        charArray[charArray.length - 1] = temp;
        return new String(charArray);
    }
}
